package org.jeecg.modules.hotel.checkmix.controller;

import java.util.*;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import org.jeecg.modules.hotel.checkmix.service.ITestBMixService;
import org.jeecg.modules.hotel.checkmix.service.IcheckmixService;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.vo.LoginUser;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.hotel.checkmix.entity.TestB;
import org.jeecg.modules.hotel.checkmix.entity.Repeat;
import  org.jeecg.modules.hotel.checkmix.vo.checkPage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 混合住宿校验
 * @Author: jeecg-boot
 * @Date:   2021-04-26
 * @Version: V1.0
 */
@Api(tags="混合住宿校验")
@RestController
@RequestMapping("/org.jeecg.modules.hotel/checkmix")
@Slf4j
public class CheckMixController {
	@Autowired
	private IcheckmixService checkService;
	@Autowired
	private ITestBMixService testBService;


	 /**
	  * 分页列表查询
	  *
	  * @param check
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 @AutoLog(value = "混合住宿校验-分页列表查询")
	 @ApiOperation(value="混合住宿校验-分页列表查询", notes="混合住宿校验-分页列表查询")
	 @GetMapping(value = "/list")
	 public Result<?> list(Repeat check,
						   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
						   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
						   HttpServletRequest req) {
		 System.out.println("-----:"+req.getParameterMap());
		 /*条件构造器 表单分页*/
		 QueryWrapper<Repeat> queryWrapper = QueryGenerator.initQueryWrapper(check, req.getParameterMap());
		 Page<Repeat> page = new Page<Repeat>(pageNo, pageSize);
		 /*条件构造器 表单分页*/

		 IPage<Repeat> queryList = checkService.checkRepeatCw(page, queryWrapper);
		 System.out.println(queryList);
		 return Result.OK(queryList);
	 }

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "混合住宿校验-通过id查询")
	@ApiOperation(value="混合住宿校验-通过id查询", notes="混合住宿校验-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		Repeat check = checkService.getById(id);
		if(check==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(check);

	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "混合校验的住宿信息通过主表ID查询")
	@ApiOperation(value="混合校验的住宿信息主表ID查询", notes="混合校验的住宿信息-通主表ID查询")
	@GetMapping(value = "/queryTestBByMainId")
	public Result<?> queryTestBListByMainId(@RequestParam(name="id",required=true) String id) {
		List<TestB> testBList = testBService.selectByMainId(id);
		return Result.OK(testBList);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param check
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, Repeat check) {
      // Step.1 组装查询条件查询数据
      QueryWrapper<Repeat> queryWrapper = QueryGenerator.initQueryWrapper(check, request.getParameterMap());
      LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

      //Step.2 获取导出数据
      //List<Repeat> queryList = checkService.list(queryWrapper);
		List<Repeat> queryList = checkService.checkRepeatCwList(queryWrapper);
		System.out.println(queryList);
      // 过滤选中数据
      String selections = request.getParameter("selections");
      List<Repeat> checkList = new ArrayList<Repeat>();
      if(oConvertUtils.isEmpty(selections)) {
          checkList = queryList;
      }else {
          List<String> selectionList = Arrays.asList(selections.split(","));
          checkList = queryList.stream().filter(item -> selectionList.contains(item.getId())).collect(Collectors.toList());
      }

      // Step.3 组装pageList
      List<checkPage> pageList = new ArrayList<checkPage>();
      for (Repeat main : checkList) {
          checkPage vo = new checkPage();
          BeanUtils.copyProperties(main, vo);
          List<TestB> testBList = testBService.selectByMainId(main.getId());
          vo.setTestBList(testBList);
          pageList.add(vo);
      }

      // Step.4 AutoPoi 导出Excel
      ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
      mv.addObject(NormalExcelConstants.FILE_NAME, "混合住宿校验列表");
      mv.addObject(NormalExcelConstants.CLASS, checkPage.class);
      mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("混合住宿校验数据", "导出人:"+sysUser.getRealname(), "混合住宿校验"));
      mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
      return mv;
    }





 }
